<template>
    <div class="login-body mainBody">
        <div class="box">
            <div class="box2">
                <div class="title">登录</div>
                <div class="box3">
                    <p class="title2">账号:</p>
                    <input type="text" v-model="account">
                </div>
                <div class="box3">
                    <p class="title2">密码:</p>
                    <input type="text" v-model="password">
                </div>
                <p>没有账号？<a @click="goToRegister">点我注册</a></p>
                <div class="buttons">
                    <button @click="gotoLogin">登录</button>
                    <button @click="getKeys">二维码登录</button>
                </div>
            </div>
        </div>
        <transition name="pushIn">
            <div class="pic" v-if="done">
            </div>
        </transition>
        <transition name="pushIn2">
            <div class="pic2" v-if="done">
                <div class="pic3">
                    <div class="pic3p">
                        <p>请打开网易云音乐扫码登录</p>
                        <svg width="20px" height="20px" viewBox="0 0 24 24" version="1.1" xmlns="http://www.w3.org/2000/svg"
                            xmlns:xlink="http://www.w3.org/1999/xlink" @click="close">
                            <g id="Page-1" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd">
                                <g id="Close">
                                    <rect id="Rectangle" fill-rule="nonzero" x="0" y="0" width="24" height="24">
                                        <title>Close</title>
                                    </rect>
                                    <line x1="16.9999" y1="7" x2="7.00001" y2="16.9999" id="Path" stroke="#0C0310"
                                        stroke-width="2" stroke-linecap="round">

                                    </line>
                                    <line x1="7.00006" y1="7" x2="17" y2="16.9999" id="Path" stroke="#0C0310"
                                        stroke-width="2" stroke-linecap="round">
                                    </line>
                                </g>
                            </g>
                        </svg>
                    </div>
                    <div class="img-bg4" v-show="codes != 802 && codes != 803">
                        <div class="img-bg">
                            <div class="img-bg2" v-show="codes == 800">
                                <p class="img-bg3">二维码过期请重新刷新</p>
                            </div>
                            <img :src="imgData" alt="">
                        </div>
                        <button @click="refresh">刷新二维码</button>
                    </div>

                    <div class="pic4" v-show="codes == 802 || codes == 803">
                        <svg width="120px" height="120px" viewBox="0 0 48 48" fill="none"
                            xmlns="http://www.w3.org/2000/svg">
                            <path fill-rule="evenodd" clip-rule="evenodd"
                                d="M24 42C33.9411 42 42 33.9411 42 24C42 14.0589 33.9411 6 24 6C14.0589 6 6 14.0589 6 24C6 33.9411 14.0589 42 24 42ZM24 44C35.0457 44 44 35.0457 44 24C44 12.9543 35.0457 4 24 4C12.9543 4 4 12.9543 4 24C4 35.0457 12.9543 44 24 44Z"
                                fill="#333333" />
                            <path fill-rule="evenodd" clip-rule="evenodd"
                                d="M34.6709 16.2585C35.0805 16.629 35.1121 17.2614 34.7415 17.6709L21.3858 32.4325L13.3095 24.7234C12.91 24.342 12.8953 23.709 13.2766 23.3095C13.658 22.91 14.291 22.8953 14.6905 23.2767L21.2809 29.5675L33.2585 16.3291C33.629 15.9196 34.2614 15.8879 34.6709 16.2585Z"
                                fill="#333333" />
                        </svg>
                        <p v-show="codes == 802">在手机上确认</p>
                        <p v-show="codes == 803">登录成功</p>
                    </div>
                </div>
            </div>
        </transition>

    </div>
</template>

<script lang="ts" setup>
import { ElMessage } from 'element-plus'
import { login, getKey, createKey, checkKey } from '../../utils/api';
import router from '../../router';
import { useUserStore } from '../../stores/user'
import { ref, onUnmounted } from 'vue'

const UseUserStore = useUserStore()

let account: string;
let password: string;

const goToRegister = () => {
    router.push('/register')
}

let done = ref(false)
let key = ref<string>('') //存放key
let imgData = ref<string>('') //存放图片地址
let codes = ref<number>(0) //响应值
/* 获得key */
const getKeys = async () => {
    const { data } = await getKey()
    if (data.code == 200) {
        key.value = ''
        imgData.value = ''
        codes.value = 0

        done.value = true
        key.value = data.unikey
        getImg(key.value)
    } else {
        err('网络出错，获取二维码失败')
    }
}
/* 获得二维码地址 */
const getImg = async (key: string) => {
    const data = await createKey(key)
    // console.log(data)
    if (data.code == 200) {
        done.value = true
        imgData.value = ''
        imgData.value = data.data.qrimg
        checkImg(key)
    } else {
        err('网络出错，请重新获取二维码或重新刷新页面')
    }
}
/* 检查二维码 */
let timer: string | number | NodeJS.Timeout | undefined
const checkImg = async (key: string) => {
    timer = setInterval(async () => {
        const data = await checkKey(key)
        // console.log('@@', data)
        if (data.code == 800) {
            clearInterval(timer)
            codes.value = data.code
            return err('二维码过期')
        } else if (data.code == 803) {
            codes.value = data.code
            // localStorage.setItem('cookie', data.cookie)
            clearInterval(timer)
            UseUserStore.getUserCookie(data)
            router.push({
                name: 'home'
            })
            return ElMessage({
                message: '登录成功',
                type: 'success',
            })

        } else if (data.code == 802) {
            codes.value = data.code
        }
    }, 2000)
}
/* 刷新接口 */
const refresh = async () => {
    clearInterval(timer)
    getKeys()
}
/* 关闭 */
const close = () => {
    done.value = false
    clearInterval(timer)
}
/* 错误接口 */
const err = (msg: string) => {
    ElMessage({
        message: msg,
        type: 'warning',
    })
}

const gotoLogin = async () => {
    return ElMessage({
        message: '由于网易服务器问题，请使用二维码进行登录',
        type: 'warning',
    })
    // if (account == undefined || password == undefined) {
    //     return ElMessage({
    //         message: '账号密码不能为空',
    //         type: 'warning',
    //     })
    // }
    // router.push({
    //     name: "login",
    //     params: {
    //         phone: account,
    //         password: password
    //     },
    //     query: {
    //         phone: account,
    //         password: password
    //     }
    // })
    // data = await login(account, password)
    // console.log(data)
    // if (data.status == 200) {
    //     UseUserStore.login(data.data.token)
    //     ElMessage({
    //         message: data.message,
    //         type: 'success',
    //     })
    // } else {
    //     ElMessage({
    //         message: data.message,
    //         type: 'warning',
    //     })
    // }
}

onUnmounted(() => {
    clearInterval(timer)
});
</script>

<style lang="scss" scoped>
.login-body {
    background: url(../../assets/米津.jpeg) no-repeat;
    background-position: center;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: center;
    box-sizing: border-box;
    position: relative;
    overflow: hidden;

    .box {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 350px;
        height: 350px;
        color: white;
        z-index: 10;
        backdrop-filter: blur(8px);
        border-radius: 20px;
        border-top: 1px solid rgba(255, 255, 255, 0.3);
        border-left: 1px solid rgba(255, 255, 255, 0.3);
        border-bottom: 1px solid rgba(255, 255, 255, 0.2);
        border-right: 1px solid rgba(255, 255, 255, 0.2);
        background: var(--table);

        .box2 {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            width: 80%;

            .title {
                font-size: 1.5em;
                font-weight: 800;
            }

            .box3 {
                width: 80%;
                margin-top: 15px;
                margin-bottom: 15px;

                .title2 {
                    font-size: 0.8em;
                    margin-bottom: 5px;
                }

                input {
                    width: 100%;
                    height: 40px;
                    padding-left: 10px;
                    padding-right: 10px;
                    color: rgba(255, 255, 255, 0.9);
                    font-size: 16px;
                    background-color: rgba(255, 255, 255, 0.1);
                    border: 1px solid rgba(255, 255, 255, 0.5);
                    border-radius: 5px;
                }
            }

            .buttons {
                display: flex;
                width: 100%;

                button {
                    margin-right: 5px;
                    margin-top: 15px;
                    width: 100%;
                    height: 35px;
                    border-radius: 20px;
                    font-size: 16px;
                    font-weight: 400;
                }
            }
        }
    }

    .pic {
        width: 100%;
        height: 100%;
        background-color: var(--loginBackground);
        // background-color: antiquewhite;
        position: absolute;
        z-index: 10;
        display: flex;
        justify-content: center;
        align-items: center;
        z-index: 2;
    }

    .pic2 {
        position: absolute;
        border-radius: 20px;
        height: 370px;
        width: 350px;
        background-color: rgb(255, 255, 255);
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        align-items: center;
        z-index: 10;

        .pic3 {
            margin-top: 10px;
            width: 100%;
            display: flex;
            flex-direction: column;
            justify-content: space-around;
            align-items: center;

            .pic3p {
                width: 80%;
                display: flex;
                justify-content: space-around;
                margin-bottom: 20px;
                text-align: center;
            }

            svg {
                cursor: pointer;
            }

            button {
                width: 120px;
                height: 30px;
                border-radius: 20px;
                background-color: var(--loginBackground);
                color: rgb(255, 255, 255);
                font-weight: 600;

                &:hover {
                    background-color: rgb(17, 13, 13);
                    // color: rgb(251, 251, 251);
                }
            }

            .img-bg4 {
                display: flex;
                align-items: center;
                flex-direction: column;

                .img-bg {
                    width: 260px;
                    height: 260px;
                    position: relative;
                    margin-bottom: 10px;

                    img {
                        width: 100%;
                        height: 100%;
                        margin-bottom: 10px;
                        z-index: 12;
                    }

                    .img-bg2 {
                        width: 100%;
                        height: 100%;
                        background-color: var(--loginBackground);
                        position: absolute;
                        z-index: 13;

                        .img-bg3 {
                            width: 100px;
                            position: absolute;
                            left: 50%;
                            top: 50%;
                            transform: translate(-50%, -50%);
                            font-size: 20px;
                            color: rgb(252, 252, 252);
                        }
                    }
                }
            }

            .pic4 {
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                width: 260px;
                height: 260px;
                font-size: 20px;

                p {
                    margin-top: 20px;
                }
            }
        }
    }
}

.pushIn-enter-active {
    animation: slideFade-dialog-fade-in 0.5s ease;
}

.pushIn-leave-active {
    animation: slideFade-dialog-fade-out 0.5s ease forwards;
}

@keyframes slideFade-dialog-fade-in {
    0% {
        // transform: translate3d(0, 50%, 0);
        opacity: 0;
    }

    100% {
        // transform: translate3d(0, 0, 0);
        opacity: 1;
    }
}

@keyframes slideFade-dialog-fade-out {
    0% {
        // transform: translate3d(0, 0, 0);
        opacity: 1;
    }

    100% {
        // transform: translate3d(0, 10%, 0);
        opacity: 0;
    }
}

.pushIn2-enter-active {
    animation: slideFade-dialog-fade-in2 0.5s ease;
}

.pushIn2-leave-active {
    animation: slideFade-dialog-fade-out2 0.5s ease forwards;
}

@keyframes slideFade-dialog-fade-in2 {
    0% {
        transform: translate3d(0, -10%, 0);
        opacity: 0;
    }

    100% {
        transform: translate3d(0, 0, 0);
        opacity: 1;
    }
}

@keyframes slideFade-dialog-fade-out2 {
    0% {
        transform: translate3d(0, 0, 0);
        opacity: 1;
    }

    100% {
        transform: translate3d(0, -10%, 0);
        opacity: 0;
    }
}
</style>